#!/usr/bin/env python3.8
# -*- encoding: utf-8 -*-
'''
@File    :   fake.py
@Time    :   2021/10/10 12:22:40
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib
import random

from faker import Faker
from sqlalchemy.exc import IntegrityError

from app.ext import db
from app.models import User,Role

#使用中文
# fake = Faker(locale='zh_CN')
fake = Faker()

def fake_role():
    role_admin = Role(name = 'Admin')
    role_user = Role(name = 'User')
    role_moderator = Role(name = 'Moderator')
    role_none = Role(name = 'None')
    #提交相关用户
    db.session.add(role_admin)
    db.session.add(role_user)
    db.session.add(role_moderator)
    db.session.add(role_none)

    db.session.commit()



def fake_user(count=10):
    for i in range(count):
        user = User(
                    username=fake.user_name(),
                    email=fake.email(),
                    real_name = fake.name(),
                    about_me=fake.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None),
                    last_seen=fake.date_time_this_year(),
                    role = Role.query.filter_by(name = 'User').first(),
                    confirmed = True, 
                    site = fake.domain_name(),
                    gender= 'male',
                    )
        user.set_password('123456')
        user.generate_avatar()
        db.session.add(user)
    try:
        db.session.commit()
    except IntegrityError:
        db.session.rollback()


